package com.gxa.ehome.web.servlet;

import com.alibaba.fastjson.JSON;
import com.gxa.ehome.dao.AdminDao;
import com.gxa.ehome.dao.UserDao;
import com.gxa.ehome.dao.impl.UserDaoBaseImpl;
import com.gxa.ehome.dao.impl.UserDaoImpl;
import com.gxa.ehome.entity.Admin;
import com.gxa.ehome.entity.User;
import com.gxa.ehome.entity.UserBase;
import com.gxa.ehome.exception.BusinessException;
import com.gxa.ehome.service.UserService;
import com.gxa.ehome.service.UserServiceBase;
import com.gxa.ehome.service.impl.UserServiceBaseImpl;
import com.gxa.ehome.service.impl.UserServiceImpl;
import com.gxa.ehome.util.JsonResult;
import com.gxa.ehome.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

//servlet上通过.do让过滤器匹配,好处
@WebServlet(name = "UserServlet", urlPatterns = "/user")
public class UserServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 处理请求中的中文, 使用UTF-8编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");   // 服务器端的字符集设置(影响流)- java代码相关
        response.setHeader("Content-Type", "text/html;charset=UTF-8"); // 浏览器行为

        JsonResult jsonResult = new JsonResult();


        //获取前端请求头数据
//        String method2 = request.getHeader("method2");
//        System.out.println("method2:"+method2);

        String method = request.getHeader("method");
//        method="selectByPhone";
        //自定义请求头
        switch (method) {
            case "selectByPhone": {
                String phone = request.getParameter("phone");
//               String phone="132589";
                try {
                    User user = userService.selectByPhone(phone);
                    UserDao userDao=new UserDaoImpl();
                    Integer count = userDao.getCount();

                    if (user != null) {
                        jsonResult.setCode("1");
                        jsonResult.setMsg("电话号码已注册,请重新输入");
//                        jsonResult.setValue("count",count);
                    } else {
//                        jsonResult.setCode("0");
                        response.sendRedirect("/sms");
                        //转发servlet
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;

            }
            case "login": {
                String phone = request.getParameter("phone");
                try {
                    User user = userService.selectByPhone(phone);
                    if (user!=null){
                        jsonResult.setCode("0");
                        jsonResult.setMsg("登录成功");
                    }else {
                        jsonResult.setCode("1");
                        jsonResult.setMsg("没有此用户,登录失败,请注册");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            }
            case "register": {
                String vUpcode = request.getParameter("vUpcode");
                String formData = request.getParameter("formData");
                User user = JSON.parseObject(formData, User.class);
                user.setRegTime(new Date());
                HttpSession session = request.getSession();
                String vcode = (String) session.getAttribute("vcode");
                if (vUpcode.equals(vcode)) {
                    try {
                        Integer register = userService.register(user);
                        if (register > 0) {
                            jsonResult.setCode("0");
                            jsonResult.setMsg("注册成功,可直接登录");
                        } else {
                            jsonResult.setCode("1");
                            jsonResult.setMsg("发生了未知注册失败,请重新注册");
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    jsonResult.setCode("1");
                    jsonResult.setMsg("验证码输入不正确,请重新输入");
                }

                break;
            }
            case "selectByPage": {
                String page = request.getParameter("page");
                String limit = request.getParameter("limit");
                try {
                    List<User> list = userService.selectAllBypage(Integer.parseInt(page),Integer.parseInt(limit));
                    jsonResult.setCode("0");
                    jsonResult.setMsg("查询成功");
                    jsonResult.setData(list);
                    jsonResult.setValue("count",15);

                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
            }
            case "deleteById": {
//                String uid = request.getParameter("uid");
//                try {
//                    Integer integer = userServiceBase.deleteById(Integer.parseInt(uid));
//                    if(integer==1){
//                        jsonResult.setCode("1");
//                        jsonResult.setMsg("删除成功");
//                    }else{
//                        jsonResult.setCode("0");
//                        jsonResult.setMsg("删除失败,请检查id");
//                       /* BusinessException businessException = new BusinessException("删除失败,请检查id");
//                        request.setAttribute("e",businessException);
//                        // 统一交给ExceptionHandlerServlet处理
//                        this.getServletContext().getRequestDispatcher("/exception").forward(request,response)*/;
//                    }
//                } catch (Exception e) {
//                    jsonResult.setCode("0");
//                    jsonResult.setMsg("系统发送未知错误,请稍后再试");
//                }
                break;
            }
            case "update": {
//                String uid = request.getParameter("uid");
//                String name = request.getParameter("name");
//                String pwd = request.getParameter("pwd");
//                //String username = request.getParameter("username");  用户名,花钱修改
//                //生成一个组装了所有修改字段值的对象
//                UserBase user = new UserBase();
//                user.setName(name);
//                user.setPwd(pwd);
//                try {
//                    userServiceBase.update(Integer.parseInt(uid),user);
//                    jsonResult.setCode("1");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
                break;
            }
            default:
                break;
        }

//        System.out.println(JSON.toJSONString(jsonResult.getValues()));
        ResponseUtil.send(response, JSON.toJSONString(jsonResult.getValues()));


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    // alt+enter: 纠错
//    public User login(String username, String pwd) {
//
//        return null;
//    }
}
